const { add, find, update, del, addAll } = require("../../server")
const punch = require('../../model/punch')
const punch1 = require('../../model/punch1')


//查询全部数据 /admin/punch/getpunchAll
const getCtr = async (req, res, next) => {
  try {
    let data = await find(punch)
    res.send({
      code: 200,
      data,
    })
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: '数据查询失败',
    })
  }
}

//模糊查询
const getCtr1 = async (req, res, next) => {
  let tiao = req.query
  try {
    let data = await find(punch, {
      nickname: tiao.nickname
    })
    let data1 = await find(punch, {
      attendance_type: tiao.attendance_type
    })
    let data2 = await find(punch, {
      nickname: tiao.nickname,
      attendance_type: tiao.attendance_type
    })
    if (tiao.nickname == '' && tiao.attendance_type != '') {
      res.send({
        code: 200,
        data: data1
      })
    } else {
      if (tiao.nickname != '' && tiao.attendance_type == '') {
        res.send({
          code: 200,
          data: data
        })
      } else {
        res.send({
          code: 200,
          data: data2
        })
      }
    }
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: '数据查询失败',
    })
  }
}

//上班打卡  /admin/punch/postPunch
const postCtr = async (req, res, next) => {
  let data = req.body
  try {
    await add(punch, data)
    res.send({
      code: 200,
      message: '上班打卡成功'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '上班打卡失败'
    })
  }
}


//批量插入  /admin/punch/postPunch
const postCtr1 = async (req, res, next) => {
  let data = req.body
  try {
    await addAll(punch, data)
    res.send({
      code: 200,
      message: '上班打卡成功'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '上班打卡失败'
    })
  }
}


// 下班打卡
const putCtr = async (req, res, next) => {
  let data = req.body
  try {
    let bob = await update(punch, {
      time_end: data.time_end,
      address_end: data.address_end
    }, {
      id: data.id
    })
    if (bob == 0) {
      res.send({
        code: 400,
        message: '下班打卡失败，请联系管理员'
      })
      return;
    }
    res.send({
      code: 200,
      message: '下班打卡成功'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '下班打卡失败，请联系管理员'
    })
  }

}

// 打卡信息修改
const putCtr1 = async (req, res, next) => {
  let data = req.body
  try {
    let bob = await update(punch, {
      address_end: data.address_end,
      address_start: data.address_start,
      attendance_type: data.attendance_type,
      nickname: data.nickname,
    }, {
      id: data.id
    })
    if (bob == 0) {
      res.send({
        code: 400,
        message: '未进行修改操作'
      })
      return;
    }
    res.send({
      code: 200,
      message: '修改成功'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '修改失败，请联系超级管理员'
    })
  }

}

// 删除数据
const delCtr = async (req, res, next) => {
  try {
    let bob = await del(punch, {
      id: req.body.id
    })
    if (bob == 0) {
      res.send({
        code: 400,
        message: '删除失败，请联系程序员'
      })
      return;
    }
    res.send({
      code: 200,
      message: '删除成功'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '删除失败，请联系程序员'
    })
  }
}

//回收站
//查询全部数据 
const getRecycle = async (req, res, next) => {
  try {
    let data = await find(punch1)
    res.send({
      code: 200,
      data,
    })
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: '数据查询失败',
    })
  }
}
//模糊查询
const getRecycles = async (req, res, next) => {
  let tiao = req.query
  try {
    let data = await find(punch1, {
      nickname: tiao.nickname
    })
    let data1 = await find(punch1, {
      attendance_type: tiao.attendance_type
    })
    let data2 = await find(punch1, {
      nickname: tiao.nickname,
      attendance_type: tiao.attendance_type
    })
    if (tiao.nickname == '' && tiao.attendance_type != '') {
      res.send({
        code: 200,
        data: data1
      })
    } else {
      if (tiao.nickname != '' && tiao.attendance_type == '') {
        res.send({
          code: 200,
          data: data
        })
      } else {
        res.send({
          code: 200,
          data: data2
        })
      }
    }
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: '数据查询失败',
    })
  }
}
// 删除数据
const delRecycles = async (req, res, next) => {
  try {
    let bob = await del(punch1, {
      id: req.body.id
    })
    if (bob == 0) {
      res.send({
        code: 400,
        message: '删除失败，请联系程序员'
      })
      return;
    }
    res.send({
      code: 200,
      message: '删除成功'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '删除失败，请联系程序员'
    })
  }
}
//新增
const postRecycles = async (req, res, next) => {
  let data = req.body
  try {
    await add(punch1, data)
    res.send({
      code: 200,
      message: '上班打卡成功'
    })
  } catch (error) {
    res.send({
      code: 400,
      message: '上班打卡失败'
    })
  }
}
// 删除数据
const delRecycles1 = async (req, res, next) => {
  try {
    let bob = await del(punch1, {
      id: req.body.id
    })
    if (bob == 0) {
      res.send({
        code: 400,
        message: '恢复失败，请联系程序员'
      })
      return;
    }
    res.send({
      code: 200,
      message: '恢复成功'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '恢复失败，请联系程序员'
    })
  }
}
module.exports = {
  getCtr, postCtr, putCtr, delCtr, getCtr1, putCtr1,getRecycle,getRecycles,delRecycles,postRecycles,delRecycles1,
    postCtr1
}